package com.sgb.kjwl.viewmodel.self.website;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.WebsiteQualityEntity;
import com.sgb.kjwl.model.entity.self.WebsiteTypeEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.websiteManage.WebsiteQualityUploadImgActivity3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WebsiteRequalityUploadModel3 extends BaseViewModel {

    private WebsiteQualityUploadImgActivity3 activity;
    private SelfRepertory repertory;

    public ObservableField<WebsiteQualityEntity> detailEntity = new ObservableField<>(new WebsiteQualityEntity());

    public WebsiteRequalityUploadModel3(WebsiteQualityUploadImgActivity3 activity3, SelfRepertory tr){
        this.activity = activity3;
        this.repertory = tr;
    }

    public void saveQualityDetail(WebsiteQualityEntity en){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().editWebsiteQuality(en).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(activity, "保存成功");
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void deleteQuality(String id){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().deleteWebsiteQuality(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(activity, "删除成功");
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getQualityRange(){
        activity.showRangeData();
//        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
//        APIManager.getInstance().getMailListAPI().getWebsitePhoto(id).enqueue(new Callback<BaseEntity>() {
//            @Override
//            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    activity.showRangeData();
//                } else {
//                    MToast.showToast(activity, response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity> call, Throwable t) {
//                t.printStackTrace();
//                DialogHelper.dismissProgressDialog();
//            }
//        });
    }

    public void getQualityType(){
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getMailListAPI().getWebsiteQualityType().enqueue(new Callback<BaseEntity<List<WebsiteTypeEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<WebsiteTypeEntity>>> call, Response<BaseEntity<List<WebsiteTypeEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showTypeData(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<WebsiteTypeEntity>>> call, Throwable t) {
                t.printStackTrace();
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void uploadMutiFileModel(File file) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + 1 + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中"+1+"/"+1+"...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.uploadImgSuccess(response.body().getData().toString());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
